<template>
  <div class="djsxz">
    <div class="items-center" style="justify-content: space-between">
      <div class="djsxz-time">
        {{ useDateFormat(serverTime, "MM-DD HH:mm:ss:SSS").value }}
      </div>
      <van-button class="djsxz-btn" @click="openCardBefor">下注</van-button>
      <van-button class="djsxz-btn" @click="openCard">开卡</van-button>
    </div>
  </div>
</template>
<script setup name="djsxz">
import { useDateFormat } from "@vueuse/core";
import { formatToTimeZone } from "@/utils/format";
import {
  getLotterySessionInfo,
  lotteryBetting,
  getLotterySimpleInfo,
  getMyCard,
  openMyCard,
} from "@/api/lottery";
import { useGlobalStore } from "~~/store/global";

const lottery = ref();
let betSpecTime = 9 * 60 * 1000; //9 分钟
const serverTime = ref(0);

/**
 * @description: 下注接口
 */
let costAmount = 0;
const lotteryBettingRequest = () => {
  lotteryBetting({
    teamConfigId: lottery.value.teamConfigId || 2, // 队伍 id
    exchangeBalanceAmount: undefined, // 充值场景-才需要传
    costAmount: costAmount || lottery.value.costAmount || "100.1",
  });
};

/**
 * @description: 间隔 9 分钟下注一次
 */
let isOnejdTime = null;
let betMaxNums = 3; // 第一节阶段:最大下注上限
const djsxz = () => {
  if (!!isOnejdTime) return;
  isOnejdTime = setInterval(() => {
    console.log("下注了--", isOnejdTime);
    betMaxNums -= 1;
    if (betMaxNums >= 1) lotteryBettingRequest();
  }, betSpecTime);
  // 默认-立刻下一注
  setTimeout(() => {
    lotteryBettingRequest();
  }, 1000);
};

/**
 * @description: 初始化定时器: 方便其他地方统一修改数据
 */
const endTime = new Date("2024/08/26").getTime();

// 查询信息
let freeAmount = 0;
let sessionDrawCount = 0;

//  获取下注 bdc 和账户余额,以及-已下注次数
const getLotterySimpleInfoRequest = () => {
  if (!lottery.value) return;
  return getLotterySimpleInfo({ sessionId: lottery.value.sessionId }).then(
    (response) => {
      if (response) {
        freeAmount = response.freeAmount;
        sessionDrawCount = response.sessionDrawCount; // 已下注次数
        costAmount = response.costAmount; // 下注花费
      }
    }
  );
};
const getLotterySessionInfoRequest = () => {
  if (lottery.value) return;
  const weekObj = { Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6, Sun: 7 };
  const { week } = formatToTimeZone(
    new Date(serverTime.value),
    "Asia/Jakarta",
    false
  );
  let dayIndex = weekObj[week];
  getLotterySessionInfo({
    selectId: dayIndex,
  }).then((res) => {
    console.log("--res", res);
    lottery.value = res;
    getLotterySimpleInfoRequest();
  });
};

/**
 * @description: px下注
 */
let syBets = ref(0);
let opencardTime = null;
const openCardBefor = async () => {
  // if (freeAmount < 200 || !lottery.value) return;
  // await getLotterySimpleInfoRequest();
  // syBets.value = 50 - Number(sessionDrawCount);
  syBets.value = 45 
  if (!opencardTime) {
    // 一秒下一注
    opencardTime = setInterval(() => {
      if (syBets.value >= 1) {
        if (
          Number(serverTime.value) > Number(lottery.value.firstStageEndTime) &&
          Number(serverTime.value) < Number(lottery.value.pkEndTime)
        ) {
          lotteryBettingRequest();
          syBets.value -= 1;
        }
      }
    }, 1288);
  }
};

let pkCards = [];
let opencardTime1 = null;
const openCard = () => {
  getMyCard().then((res) => {
    if (res) {
      pkCards = res.pendingOpenList;
    }
  });
  if (!opencardTime1) {
    opencardTime1 = setInterval(() => {
      if (pkCards && pkCards.length >= 1) {
        const card = pkCards.pop();
        openMyCard({ id: card.id });
      }
    }, 1000);
  }
};

/**
 * @description: 初始化定时器: 方便其他地方统一修改数据
 * 1. 时间进度逻辑实现
 */
// 创建一个特定时间的 Date 对象; 用于解决serverTime 初始值小于 1000 的场景
const ckbzTime = new Date("2024/01/01").getTime();
const globalStore = useGlobalStore();
const serverTimeWatch = watch(
  () =>globalStore.$state.serverTime,
  (newValue, oldValue) => {
    if (newValue > ckbzTime) {
      serverTime.value = newValue;
      getLotterySessionInfoRequest();
      if (lottery.value) {
        if (
          Number(serverTime.value) > Number(lottery.value.beginTime) + 2000 &&
          Number(serverTime.value) < Number(lottery.value.firstStageEndTime)
        ) {
          // 小于-设定的截止时间
          if (Number(serverTime.value) < endTime) {
            djsxz();
          }
        }
        if (
          Number(serverTime.value) > Number(lottery.value.firstStageEndTime)
        ) {
          isOnejdTime && clearInterval(isOnejdTime);
        }
      }
    }
  },
  {
    immediate: true,
  }
);

// 页面属性
onMounted(() => {
  getLotterySessionInfoRequest();
});

onBeforeUnmount(() => {
  // 1.注销通知
  // moneyIntervalTime && clearInterval(moneyIntervalTime);
  serverTimeWatch && serverTimeWatch();
  opencardTime && clearInterval(opencardTime);
  opencardTime1 && clearInterval(opencardTime1);
  isOnejdTime && clearInterval(isOnejdTime);
});
</script>
<style lang="scss" scoped>
.djsxz {
  position: relative;
  margin: 15 * 2px 0 * 2px;
  width: calc(100% - 18 * 2px - 6 * 2px);
  &-btn {
  }
}
</style>
